package com.tongzhou.client;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IAccountDAO;
import com.tongzhou.tongzhou.account.dao.impl.AccountDAO;
import com.tongzhou.tongzhou.account.model.Account;
import com.tongzhou.util.Config;

//报价接口
public class ClientQuot2 {
	IAccountDAO dao=new AccountDAO();
	public String getInfo(String json,HttpServletRequest request){
		JSONObject jsonObject = JSONObject.fromObject( json );
		String companyNum=null;
		String rid="";
		Integer type=null;
		if(jsonObject.get("companyNum")!=null){
			companyNum=(String) jsonObject.get("companyNum");
		}
		if(jsonObject.get("rid")!=null){
			rid=(String) jsonObject.get("rid");
		}
		if(jsonObject.get("type")!=null){
			type=(Integer) jsonObject.get("type");
		}
		StringBuffer str=new StringBuffer();
		if(type==null||((companyNum==null||companyNum.equals(""))&&(rid==null||rid.equals("")))){
			return "{'success':0,'msg':'输入参数有误!'}";
		}else{
			if(companyNum!=null&&!companyNum.equals("")){
				Account account;
				try {
					account = dao.getAccountByNum(companyNum);
					if(account!=null){
						str.append("{'success':0,'msg':{'content':'"+Config.domain_name+"/TongZhou/admin/tongzhou/account!listAccountTemp.action?account_id="+account.getId()+"'}}");
					}else{
						str.append("{'success':0,'msg':'没有对应的报价信息!'}");
					}
					return str.toString();
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
			if(rid!=null&&!rid.equals("")){
				str.append("{'success':0,'msg':{'content':'"+Config.domain_name+"/TongZhou/admin/tongzhou/quotRegion!listRegionTemp.action?region_id="+rid+"'}}");
				return str.toString();
			}
			
		}
		return "";
	}
	public static void main(String[] args) {
		String url="http://www.tongzhoudg.com/tongzhouClient/client.do?method=1&json={'companyName':'abc','companyNum':'abc001'}";
		System.out.println(GetNetData(url,1));
	}
	
	private static String GetNetData(String url, int flag)
	{
		//连接网络获取数据
		URL urls 						= null;
		HttpURLConnection connection 	= null;
		
		//获取网络数据
		try
		{
			urls = new URL(url);
			
			connection = (HttpURLConnection) urls.openConnection();
			connection.setConnectTimeout(10 * 1000);//设置连接主机超时(单位：毫秒)
			connection.setReadTimeout(10 * 1000);	//设置从主机读取数据超时(单位：毫秒)
			
				return InputStreamtoString(connection.getInputStream()).trim();
//			else if (flag == RTN_IN)
//			{
//				return connection.getInputStream();
//			}
//			else
//			{
//				return null;
//			}
		}
		catch (Exception e) 
		{
			// TODO: handle exception
		}
		
		return null;
	}
	
	public static String InputStreamtoString(InputStream in) throws IOException 
	{
		StringBuffer out = new StringBuffer();
		
		byte[] buffer = new byte[1024];
		
		for (int i; (i = in.read(buffer)) != -1;) 
		{
			out.append(new String(buffer, 0, i));
		}
		
		return out.toString();
	}
	
}
